package com.thtf.strokeinquiry.app.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateConvertUtil {

    public static Logger logger = LoggerFactory.getLogger(DateConvertUtil.class);
    public final static String defaultFormmtter = "yyyy-MM-dd :HH:mm:ss";
    public final static String dateFormatter = "MMddHHmmss";
    public final static String yearFormatter = "yy";

    public static String covvert2String(Date date, String formatterStr) {
        SimpleDateFormat formatter = new SimpleDateFormat(formatterStr);
        String result = formatter.format(date);
        return result;
    }

    public static int getAge(Date birthDay) {
        Calendar cal = Calendar.getInstance();
        if (cal.before(birthDay)) {
            throw new IllegalArgumentException(
                    "The birthDay is before Now.It's unbelievable!");
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;

        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            } else {
                age--;
            }
        }
        return age;
    }

//    public static Date getBirthFromIdCard(String idCard) throws BusinessException {
//        if (idCard == null||idCard.length()<14 || idCard.length() < 15) {
//            return null;
//        }
//        String birthStr = idCard.substring(6, 14);
//        String dateFormate = "yyyyMMdd";
//        SimpleDateFormat sdf = new SimpleDateFormat(dateFormate);
//        try {
//            Date birth = sdf.parse(birthStr);
//            return birth;
//        } catch (ParseException e) {
//            e.printStackTrace();
//            logger.error(" error idCard message  []",idCard);
//            throw  new BusinessException(ServerCode.DATE_PARE_ERROR);
//        }
//    }

    public  static  void main(String args[]){
        System.out.println( DateConvertUtil.covvert2String(new Date(),"yyyy"));
    }

}
